package com.small.util;

import com.small.common.RedisPool;
import com.small.common.RedisShardedPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;

/**
 * @create: xie
 */
@Slf4j
public class RedisShradedPoolUtil {
    //exTime设置过期时间，单位是秒
    public static String setEx(String key,String value,int exTime){
        ShardedJedis jedis=null;
        String result=null;
        try {
            jedis= RedisShardedPool.getJedis();
            result= jedis.setex(key,exTime,value);
        } catch (Exception e) {
            log.error("setEx key:{} value:{} exTime{} error",key,value,exTime,e);
            return result;
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        return result;
    }
    //设置key有效期，单位是秒
    public static Long expire(String key,int exTime){
        ShardedJedis jedis=null;
        Long result=null;
        try {
            jedis= RedisShardedPool.getJedis();
            result= jedis.expire(key,exTime);
        } catch (Exception e) {
            log.error("expire key:{} error",key,e);
            return result;
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        return result;
    }

    public static String set(String key,String value){
        ShardedJedis jedis=null;
        String result=null;
        try {
            jedis= RedisShardedPool.getJedis();
            result= jedis.set(key, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error",key,value,e);
            return result;
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        return result;
    }

    //获取一个key，设置新的value，并返回旧的value，此操作具有原子性
    public static String getSet(String key,String value){
        ShardedJedis jedis=null;
        String result=null;
        try {
            jedis= RedisShardedPool.getJedis();
            result= jedis.getSet(key, value);
        } catch (Exception e) {
            log.error("getSet key:{} value:{} error",key,value,e);
            return result;
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        return result;
    }

    public static String get(String key){
        ShardedJedis jedis=null;
        String result=null;
        try {
            jedis= RedisShardedPool.getJedis();
            result= jedis.get(key);
        } catch (Exception e) {
            log.error("get key:{} error",key,e);
            return result;
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        return result;
    }

    public static Long del(String key){
        ShardedJedis jedis=null;
        Long result=null;
        try {
            jedis= RedisShardedPool.getJedis();
            result= jedis.del(key);
        } catch (Exception e) {
            log.error("del key:{} error",key,e);
            return result;
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        return result;
    }


    //setnx设置key的值为value,如果key的值存在则什么也不做
    //返回值为1表示设置了key，代表获取到了锁，返回值为0表示没获取到锁
    public static Long setnx(String key,String value){
        ShardedJedis jedis=null;
        Long result=null;
        try {
            jedis= RedisShardedPool.getJedis();
            result= jedis.setnx(key,value);
        } catch (Exception e) {
            log.error("setnx key:{} value:{} error",key,value,e);
            return result;
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        return result;
    }

}
